package gov.fgn.sijyp.general.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.util.logging.Logger;

import javax.servlet.http.Part;

public abstract class FileUploadBeanJsf extends FileUploadBean {

	private static Logger LOG = Logger.getLogger(FileUploadBeanJsf.class.getName());
	private Part file;
	private static final int EOF = -1;
	
	public FileUploadBeanJsf() {
		super();
	}
	
	public void uploadListener() {
		try {
			InputStream is = file.getInputStream();
			byte[] data = new byte[new Long(file.getSize()).intValue()];
			OutputStream os = new ByteArrayOutputStream(new Long(file.getSize()).intValue());
			
			int read = 0;
			while((read = is.read(data, 0, data.length)) == EOF) {
				os.write(data, 0, read);
			}

		    this.bienDocumento.setArchivo(data);
		   
		    this.bienDocumento.setNombre(file.getName());
		    this.bienDocumento.setMimeType(file.getContentType());
		    this.bienDocumento.setLongitud(BigDecimal.valueOf(file.getSize()));
		} catch(IOException e) {
			LOG.severe(e.getMessage());
		}
	}
	
	public void setFile(Part file) {
        this.file = file;
    }
	
	public Part getFile() {
        return file;
    }
	
	
	@Override
	public void limpiarUpload() {
		super.limpiarUpload();
		file = null;
	}
}
